#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2016 lizongzhe 
#
# Distributed under terms of the MIT license.

from Task.models import Task, Job, JobOutput
import urllib
from rest_framework import serializers

class JobOutputSerializer(serializers.ModelSerializer):
    url = serializers.SerializerMethodField()

    class Meta:
        fields = ('key', 'url')
        model = JobOutput

    def get_url(self, obj):
        return urllib.unquote_plus(obj.value.url)

class JobSerializer(serializers.HyperlinkedModelSerializer):
    url =serializers.HyperlinkedIdentityField(view_name='job-detail')
    outputs = JobOutputSerializer(many=True)
    class Meta:
        model = Job
        fields = ('url', 'name', 'date', 'outputs')


class TaskSerializer(serializers.HyperlinkedModelSerializer):
    jobs = JobSerializer(many=True)
    class Meta:
        model = Task
        fields = ('id', 'module', 'name', 'url', 'live', 'jobs')

